<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html   xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>后台管理 - 易买网</title>
<link type="text/css" rel="stylesheet" href="../css/style.css" />
<script type="text/javascript" src="../scripts/function-manage.js"></script>
	<script type="text/javascript" src="../scripts/jquery-1.8.3.js"></script>
</head>
<body style="width: 100%;">
<div class="main">
	<h2>修改新闻</h2>
	<a href="javascript:history.go(-1)"><h2><<返回上一级</h2></a>
	<div class="manage">
		<form action="../UpdateNewsServlet" enctype="multipart/form-data" method="post">
			<table class="form">
				<tr>
					<td class="field">新闻标题：<input type="text" name="enid" value="${news.enid}" hidden/></td>
					<td><input type="text" class="text" name="title" value="${news.entitle}"  required/></td>
				</tr>
				<tr>
					<td class="field">新闻内容：</td>
					<td><textarea name="content" value="${news.encontext}" style="resize: none">${news.encontext}</textarea></td>
				</tr>
				<tr>
					<td class="field">更改图片：</td>
					<td><input type="file" name="pic" value="${news.pic}">
						&nbsp;分类:
						<select name="parentClass" id="parentClass" onchange="parentChange()">
							<option value="0">请选择</option>
							<c:forEach items="${list}" var="v">
								<option value="${v.epcid}">${v.epcname}</option>
							</c:forEach>
						</select>
						&nbsp;&nbsp;子类:
						<select name="ChildClass" id="ChildClass" onchange="productshow()">
							<option value="${ChildClass.epcid}" selected="selected">${ChildClass.epcname}</option>

						</select>
					</td>
				</tr>
				<tr>
					<td class="field">图片预览：</td>
					<td><img src="fileup/${news.pic}"   width="600px" height="500px"></td>
				</tr>
				<tr>
					<td class="field">选择板块：</td>
					<td><input type="radio" name="enflag" value="0" id="cuxiao" class="bankuai" ><label for="cuxiao">促销信息</label> &nbsp;<input type="radio" name="enflag" value="1" id="gonggao" class="bankuai"> <label for="gonggao">新闻公告</label>
						&nbsp;&nbsp;
						<a href="javascript:void(0)" onclick="productshow()" >选择关联商品(无需添加请点击取消关联)</a>
						&nbsp;&nbsp;
						<input type="text" value="${product.epname}" readonly id="referencename" size="20">
                        &nbsp;&nbsp;
                        <a href="javascript:void(0)" onclick="canclerel()">取消关联</a>
						<input type="text" value="${product.productCategory.epcid}" hidden id="reference" name="epid"/>
					</td>
				</tr>
				<tr>
					<td></td>
					<td><label class="ui-blue"><input type="submit" name="submit" value="修改" /></label></td>
				</tr>
			</table>
		</form>
	</div>
</div>
<div id="detail_list" style="float: right ;position: absolute ;top: 10px ;right: 200px" >
	<table class="list" id="tbproduct">
		<tr>
			<td colspan="2" width="50%" class="bankuai" id="asd" style="background-color: #fc7e31">商品列表</td>
		<tr>
			<th>商品名称</th>
			<th>添加关联</th>
		</tr>
	</table>
	<a href="javascript:void (0)" id="firstpage" class="pages" fenye='CuXiaoFenYe'>首页</a>
	<a href="javascript:void (0)" id="prevpage" class="pages"  fenye='CuXiaoFenYe'>上一页</a>
	<a href="javascript:void (0)" id="nextpage" class="pages" fenye='CuXiaoFenYe'>下一页</a>
	<a href="javascript:void (0)" id="lastpage" class="pages" fenye='CuXiaoFenYe'>尾页</a>

</div>

</body>
</html>
<script type="text/javascript">
    var pageIndex;//当前页
    var totalPage;//总页数

	$(function () {
	    //点击版块后按钮指向变化
		function bankuaixuanze(){
	    $("[value=${news.enflag}]").attr("checked",true)
        }
        bankuaixuanze();

    });

	$(function () {
	    //商品所属类数据数据回填
		$("#parentClass [value=${news.parentClass}]").attr("selected", true);
    });





    //异步分页获取商品信息
    var getProductlist = function(curr){
        $.ajax({
            type:'POST',
            url:'../GetProductByChildrenJsonServlet',
            dataType:'JSON',
            data: {
                epcid: $("#ChildClass").val(),
                pageIndex: curr || 1
            },
            success:function(data){
                $("#tbproduct").find(".contextdata").remove();
                $("#tbproduct").find(".page").remove();
                $.each(data.productList,function (index,obj) {
                    var $newsStr="<tr class='contextdata'><td>"+ obj.epname+"</td><td><a href='javascript:void(0)' onclick='producthide("+obj.epid+',"'+obj.epname+'"'+")'>添加关联</a></td></tr>";
                    $("#tbproduct").append($newsStr);
                })
                var $pageinfor="<tr class='page'><td colspan=4>共有<span id='total_page'>"+data.totalPages+"<span>页&nbsp;"+data.totalRows+"条记录&nbsp;当前为第<span id='curr_page'>"+data.pageIndex+"</span>页</td></tr>"
                $("#tbproduct").append($pageinfor);

                totalPage = data.totalPages;
                pageIndex = data.pageIndex;
            }
        })
    }

    function producthide(id,epname){
        $("#detail_list").css("display","none");
        $("#referencename").val(epname);
        $("#reference").val(id);
    }

    function productshow(){
        $("#detail_list").css("display","block");
        getProductlist();
    }

    //异步控制子分类下拉框信息
    function parentChange(){
        var pid = $("#parentClass").val()
        $.ajax({
            type:'POST',
            url:'../ToNewsAddChildrenListServlet',
            dataType:'JSON',
            data:{
                pid:pid
            },
            success:function (data) {
                $("#ChildClass").find(".childop").remove();
                $.each(data,function (index,obj) {
                    var $str = "<option name ='childId' class='childop' value="+obj.epcid+">"+obj.epcname+"</option>"
                    $("#ChildClass").append($str);
                })
            }
        })
    }

    //促销信息分页
    function cuxiaofenye(){
        $("[fenye='CuXiaoFenYe']").unbind()
        $("[fenye='CuXiaoFenYe']").on("click",function () {
            var pageflag = $(this).attr("id")

            if (pageflag=="firstpage"){
                getCuXiaoNews(1);
            } else if (pageflag=="prevpage"){
                pageIndex--;
                if(pageIndex<1){
                    pageIndex=1;
                }
                getCuXiaoNews(pageIndex);
            } else if (pageflag == "nextpage"){
                pageIndex++;
                if(pageIndex>totalPage){
                    pageIndex=totalPage;
                }
                getCuXiaoNews(pageIndex);
            } else if(pageflag=="lastpage"){
                getCuXiaoNews(totalPage);
            }
        })
    }

    //取消关联商品
    function canclerel(){
        $("#referencename").val("");
        $("#reference").val(0);
    }




</script>